package com.docker.test.netty.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.docker.test.netty.Dao.Chat;
import com.docker.test.netty.Dao.MessageInfo;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Scanner;


public class ChatClient {

    private static Logger log = LoggerFactory.getLogger(ChatClient.class);

    private static String clientId = "9901";
    private static String targetId = "9902";

    public static void main(String[] args) {
        String url ="http://localhost:2345";
        try{
            IO.Options options = new IO.Options();
            options.transports = new String[]{"websocket"};
            options.reconnectionAttempts = 2;
            options.reconnectionDelay = 1000;//失败重连的时间间隔
            options.timeout = 500;//连接超时时间(ms)
            //par1 是任意参数
            final Socket socket = IO.socket(url+"?clientid=9901", options);

            socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

                public void call(Object... args) {
                    socket.send("hello");
                }
            });

            //自定义事件
            socket.on("message_event", new Emitter.Listener() {
                public void call(Object... objects) {
                    MessageInfo ms = JSONObject.parseObject(objects[0].toString(),MessageInfo.class);
                    log.info("{}给{}发送消息：{}",ms.getSourceClientId(),ms.getTargetClientId(),ms.getMsg());
                }
            });

            socket.on("connected", new Emitter.Listener() {
                public void call(Object... objects) {
                    System.out.println("receive connected data:" + objects[0].toString());
                }
            });

            socket.connect();
            //循环发送数据
            while (true){
                MessageInfo ms = new MessageInfo();
                ms.setSourceClientId(clientId);
                ms.setTargetClientId(targetId);

                Scanner scanner = new Scanner(System.in);
                String str = scanner.nextLine();
                ms.setMsg(str);
//                socket.emit("message_event"," 客户端在发送数据");
                socket.emit("message_event", JSONObject.toJSON(ms));
//                log.info("send message :【{}】", JSONObject.toJSONString(ms));
                Thread.sleep(2000);
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public void testxx(){
        //这是一个testxx方法

        //这是分之test_20211114写的代码

        //这是陶鹏在testxx加的新代码

        //新代码master1

        //新代码test20211114   2


        //新代码test20211114   3


        //新代码test20211114   4


        //新代码test20211114   5


    }
    public void testNew(){
        //这是一个testNew方法

        //这是陶鹏在testxx加的新代码

        //新代码test20211114   2

        //新代码test20211114   3




        //新代码test20211114   5


    }


    public void test(){
        //这就是一个测试方法
        //测试而已
        //这是test分之的代码
        //新代码master1
    }


    public void testNew2(){
        //这是一个testNew2方法
//2 3 4 5

    }

}
